package com.hualan.sfzy.controller;

import com.hualan.sfzy.entity.SysUser;

import com.hualan.sfzy.service.SysUserService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public Result login(String username, String userpwd){
        return sysUserService.login(username, userpwd);
    }

    @PostMapping
    public Result add(@RequestBody SysUser sysUser){
        int result = sysUserService.add(sysUser);
        if(result>0)
            return Result.success(result);
        return Result.error("添加失败");
    }
    @GetMapping
    public Result find(){
        List<SysUser> sysUsers = sysUserService.find();
        if(sysUsers!=null)
            return Result.success(sysUsers);
        return Result.error("查询失败");
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        SysUser sysUser = sysUserService.findById(id);
        if(sysUser!=null)
            return Result.success(sysUser);
        return Result.error("查询失败");
    }
    @GetMapping("/page")
    public Result findPage(Integer pageNum, Integer pageSize, SysUser sysUser){
        return Result.success(sysUserService.findPage(pageNum, pageSize, sysUser));
    }
    
    @PutMapping
    public Result update(@RequestBody SysUser sysUser){
        int result = sysUserService.edit(sysUser);
        if(result>0)
            return Result.success(result);
        return Result.error("更新失败");
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        int result = sysUserService.remove(id);
        if(result>0)
            return Result.success(result);
        return Result.error("删除失败");
    }
    @DeleteMapping("/batch")
    public Result deleteBatch(Integer[] ids){
        int result = sysUserService.removeBatch(ids);
        if(result>0)
            return Result.success(result);
        return Result.error("删除失败");
    }
}

